<?php

namespace App\HttpController\Admin;

use App\Model\LoginLogsModel;

class LogsController extends AuthController {

    /**
     * @title 日志记录
     * @description 登录日志记录
     * @method GET
     * @request manage/log/login
     * @param int page 页码 false 1
     * @param string ip 登录IP false ''
     * @return int id 记录ID
     * @return string username 登录账户
     * @return string password 登录密码
     * @return tinyint status 登录状态0失败1成功
     * @return string login_ip 登录IP
     * @return string login_at 登录时间
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function login(){
        $request = $this->request();
        $page = $request->getRequestParam('page') ?: 1;
        $kwd = trim($request->getRequestParam('kwd')) ?: '';
        $model = new LoginLogsModel();
        $list = $model->where(function ($query) use($kwd){
                if($kwd){
                    $query->where('login_ip',$kwd)->whereOr('username',$kwd);
                }
            })->orderBy('login_at')
            ->paginate($page);
        $this->stateToText($list['data'],[
            'status' => $model::STATUS_TEXT,
        ]);
        return $this->writeJson(0,$list);
    }

}
